<?php

namespace App\Kuafu\Order\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Order\Service\OrderDb;
use App\Kuafu\Order\Service\OrderTable;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class OrderDiscountSplitModel extends BaseModel
{
    use HasFactory, KfSoftDeletes;

//    protected $connection = 'mysqlOrder';

    public static string $tableName;
    public static string $connectionName;

    /**
     * @param $shopId
     * @return static
     */
    public static function suffix($shopId)
    {
        if (is_int($shopId) && $shopId < 0) {
            $shopId = 0;
        }

        self::$tableName      = OrderTable::orderDiscountTableName($shopId);
        self::$connectionName = OrderDb::orderDbConnection($shopId);

        return new static;
    }

    public function getTable()
    {
        return self::$tableName ?? parent::getTable();
    }

    public function getConnectionName()
    {
        return self::$connectionName ?? parent::getConnectionName();
    }


    // 插入 订单详情
    public static function insertOrderDiscount($data)
    {
        self::query()->insert($data);
    }


    // 插入 订单详情
    public static function insertOrderDiscountAfterFormat($data)
    {
        $afterData = self::formatInsertData($data);
        self::query()->insert($afterData);
    }

    public static function formatInsertData($data)
    {
        return $data;
    }

    // 插入 订单详情
    public static function updateOrderDiscount($orderId, $data)
    {
        // 删除原有折扣
        self::query()->where('orderId', (int)$orderId)->update(['isDeleted' => 1]);
        // 写入最新折扣

        self::query()->insert($data);
    }
}
